package com.yr.controller;

import com.yr.entity.order;
import com.yr.service.orderservice;
import com.yr.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/order")
public class ordercontroller {
    @Autowired
    private orderservice orderService;

    //查询订单

    @GetMapping("/list")
    public R list(){
        return R.success().data("orders",orderService.list());
    }

    //添加订单

    @PostMapping("/add")
    public R add(@RequestBody order order){
        if(orderService.save(order)) return R.success().message("添加成功！");
        else return R.error().message("添加失败！");
    }

    //修改订单
    @PostMapping("/update")
    public R update(@RequestBody order order){
        if(orderService.updateById(order)) return   R.success().message("修改成功！");
        else return R.error().message("修改失败！");
    }
    //删除订单

    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable("ids") String ids){
        String[] str = ids.split(",");
        List<String> IDs = Arrays.asList(str);
        if(orderService.removeByIds(IDs)) return  R.success().message("删除成功!") ;
        else return R.error().message("删除失败!");
    }
}
